import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_core_kit/widget/keyboard/view_pwdfield.dart';

import 'key_event.dart';
import 'keyboard_item.dart';

class CoreKitKeyboard extends StatefulWidget {
  final onKeyDown;
  final onResult;
  final autoBack;
  final pwdLength;
  final double? keyHeight;
  final Widget? delwidget;
  final Widget? confirmwidget;
  final Widget? subtitleWidge;
  final tipsText;
  final String? deltext;
  final String? confirtext;
  final String? forgettext;
  final bool? showforgettext;
  final GestureTapCallback? onForget;
  final String? resettext;
  final bool? showresettext;
  final GestureTapCallback? onReset;

  const CoreKitKeyboard(
      {this.onKeyDown,
      this.onResult,
      this.autoBack = false,
      this.pwdLength = 6,
      this.keyHeight = 48,
      this.deltext,
      this.delwidget,
      this.confirtext,
      this.tipsText,
      this.confirmwidget,
      this.subtitleWidge,
      this.forgettext,
      this.showforgettext = false,
      this.resettext,
      this.showresettext = false,
      this.onForget,
      this.onReset});

  @override
  _CoreKitKeyboardState createState() => _CoreKitKeyboardState();
}

class _CoreKitKeyboardState extends State<CoreKitKeyboard> {
  String data = "";

  List<String> keyList = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "del",
    "0",
    "commit"
  ];

  @override
  void initState() {
    super.initState();
    if (widget.autoBack) {
      keyList[9] = "";
      keyList[11] = "del";
    }
  }

  @override
  Widget build(BuildContext context) {
    double pwdHeight = 140;
    if (widget.subtitleWidge != null) {
      pwdHeight = 190;
    }

    return Container(
      height: MediaQuery.of(context).size.height,
      width: double.infinity,
      color: Colors.transparent,
      child: Stack(
        children: [
          Align(
            alignment: Alignment.center,
            child: pwdWidget(pwdHeight),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: keyboardWidget(),
          ),
        ],
      ),
    );
  }

  Widget pwdWidget(double pwdHeight) {
    return Container(
      width: double.infinity,
      height: pwdHeight,
      decoration: BoxDecoration(
        color: CupertinoTheme.of(context).scaffoldBackgroundColor,
        borderRadius: BorderRadius.circular(6),
      ),
      margin: EdgeInsets.all(20),
      child: Stack(
        children: <Widget>[
          Align(
            child: IconButton(
              icon: Icon(Icons.close, size: 28),
              onPressed: () => widget.onKeyDown(KeyDownEvent("close")),
            ),
            alignment: Alignment.topRight,
          ),
          Container(
            padding: EdgeInsets.all(10),
            alignment: Alignment.center,
            child: Column(
              children: <Widget>[
                SizedBox(height: 10),
                Text(
                  widget.tipsText ?? "Please enter your payment code",
                  style: TextStyle(fontSize: 16),
                  textAlign: TextAlign.center,
                ),
                SizedBox(height: 10),
                Container(
                  width: 250,
                  height: 40,
                  margin: EdgeInsets.only(top: 10),
                  child: CustomPwdField(data, widget.pwdLength),
                ),
                if (widget.subtitleWidge != null) widget.subtitleWidge!,
                if (widget.showforgettext! || widget.showresettext!)
                  SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    if (widget.showforgettext!)
                      InkWell(
                        onTap: widget.onForget,
                        child: Text(
                          widget.forgettext ?? "Forgetting payment password",
                          style: TextStyle(color: Colors.grey, fontSize: 12),
                        ),
                      ),
                    if (widget.showresettext!)
                      InkWell(
                        onTap: widget.onReset,
                        child: Text(
                          widget.resettext ?? "Reset the payment password",
                          style: TextStyle(color: Colors.grey, fontSize: 12),
                        ),
                      )
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget keyboardWidget() {
    return Container(
      color: CupertinoTheme.of(context).scaffoldBackgroundColor,
      height: keyList.length / 3 * widget.keyHeight! +
          MediaQuery.of(context).padding.bottom,
      padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom),
      child: Wrap(
        children: keyList.map((item) {
          String title = item;
          if (item == 'del' && widget.deltext != null) {
            title = widget.deltext!;
          }
          if (item == 'commit' && widget.confirtext != null) {
            title = widget.confirtext!;
          }
          return KeyboardItem(
            keyHeight: widget.keyHeight,
            text: title,
            callback: () => onKeyDown(context, item),
          );
        }).toList(),
      ),
    );
  }

  void onKeyDown(BuildContext context, String text) {
    if ("commit" == text && data.length >= 6) {
      widget.onResult(data);
      return;
    }
    if ("del" == text && data.length > 0) {
      setState(() {
        data = data.substring(0, data.length - 1);
      });
    }
    if (data.length >= widget.pwdLength) {
      return;
    }
    setState(() {
      if ("del" != text && text != "commit") {
        data += text;
        widget.onKeyDown(KeyDownEvent(text));
      }
    });
    if (data.length == widget.pwdLength && widget.autoBack) {
      widget.onResult(data);
    }
  }
}
